var fs = require('fs');
var path = require('path');
var uuid = require('node-uuid');

var Contract = require('./common/jsContract');
var promisify = require('./common/promisify');
(<any>global).Contract = Contract;

function createId() {
    var orderId = uuid.v4().replace(/-/g, '');
    var convertHexToBase36 = function (hex) {
        var big = parseInt(hex, 16);
        return big.toString(36);
    }
    return convertHexToBase36(orderId);
}

function mkdirsSync(dirname, mode) {
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirsSync(path.dirname(dirname), mode)) {
            fs.mkdirSync(dirname, mode);
            return true;
        }
    }
}

function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes == 0) return '0 Byte';
    var i = parseInt((Math.floor(Math.log(bytes) / Math.log(1024))).toString());
    return (<any>Math).round(bytes / Math.pow(1024, i), 2) + sizes[i];
}

function secondsToDays(seconds) {
    var days = Math.floor(seconds / 86400);
    var hours = Math.floor((seconds % 86400) / 3600);
    var minutes = Math.floor(((seconds % 86400) % 3600) / 60);
    var numseconds = Math.floor(((seconds % 86400) % 3600) % 60);
    return days + "天" + hours + "小时" + minutes + "分" + numseconds + "秒";
}

module.exports = {
    createId: createId,
    mkdirsSync: mkdirsSync,
    bytesToSize: bytesToSize,
    secondsToDays: secondsToDays
}
